package com.geostar.constructionland.analyse.service.automatic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.service.automatic.entity.AttributeInfo;
import com.geostar.constructionland.analyse.service.automatic.entity.CoordinateFile;
import com.geostar.constructionland.analyse.service.automatic.entity.ProjectInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yelll
 * @since 2020-7-8 14:24
 * 坐标文件解析
 **/
@Component
@Slf4j
public class CoordinateFileProxy {
    @Autowired
    ObjectMapper objectMapper;
    @Value("${automatic.coordinate-wkid}")
    String coordinate_wkId;

    public CoordinateFile proxyFileContent(String fileContent){
        Map<String,Object> allAttribute = new HashMap<>();
        String[] firstSplit = fileContent.split("\\[属性描述\\]");
        ProjectInfo projectInfo = proxyProjectInfo(firstSplit[0].replaceAll("\\[项目信息\\]",""),allAttribute);
        String[] secondSplit = firstSplit[1].split("\\[地块坐标\\]");
        AttributeInfo attributeInfo = proxyAttributeInfos(secondSplit[0],allAttribute);
        List<Map<String,Object>> coordinateList = getCoordinates(secondSplit[1]);
        CoordinateFile coordinateFile = new CoordinateFile();
        coordinateFile.setAttributeInfo(attributeInfo);
        coordinateFile.setProjectInfo(projectInfo);
        coordinateFile.setLandCoordinates(coordinateList);
        coordinateFile.setAllAttribute(allAttribute);
        return coordinateFile;
    }


    /***
     * 解析项目信息
     * @param fileContent
     * @return
     */
    public ProjectInfo proxyProjectInfo(String fileContent,Map<String,Object> allAttribute){
        ProjectInfo projectInfo = new ProjectInfo();
        String[] details = fileContent.split("\r\n");
        for(String proInfo : details){
            if (StringUtils.isBlank(proInfo) || !proInfo.contains("=") || proInfo.length() <=2) {
                continue;
            }
            String[] xx = proInfo.split("=");
            allAttribute.put(xx[0],xx.length>1?xx.length>1?xx[1]:"":"");
            if(xx[0].equals("项目名称")){
                projectInfo.setXmmc(xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("项目所在县区代码")){
                projectInfo.setXmszxqdm(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("项目所在市县名称")){
                projectInfo.setXmszsxmc(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("项目类别")){
                projectInfo.setXmlb(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("项目投资额")){
                projectInfo.setXmlb(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("开发用途")){
                projectInfo.setKfyt(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("总用地面积")){
                projectInfo.setZydmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("占用基本农田面积")){
                projectInfo.setZyjbntmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("农用地面积")){
                projectInfo.setNydmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("耕地面积")){
                projectInfo.setGdmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("园地面积")){
                projectInfo.setYdmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("林地面积")){
                projectInfo.setLdmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("养殖水面面积")){
                projectInfo.setYzsmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("其他农用地面积")){
                projectInfo.setQtnydmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("带K地类面积")){
                projectInfo.setDkdlmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("建设用地面积")){
                projectInfo.setJsydmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("未利用地面积")){
                projectInfo.setWlydmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("围填n海面积")){
                projectInfo.setWthmj(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("是否增减挂钩项目")){
                projectInfo.setSfzjggxm(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("是否属于增减挂钩中发展改革小城镇试点项目")){
                projectInfo.setSfsyzjggzfzggxczsdxm(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("是否属于建设用地指标调整项目")){
                projectInfo.setSfsyjsydzbtzxm(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("备注")){
                projectInfo.setRemark(xx.length>1?xx[1]:"");
            }
        }
        return projectInfo;
    }

    /**
     * 解析属性信息
     * @param fileContent
     * @return
     */
    public AttributeInfo proxyAttributeInfos(String fileContent,Map<String,Object> allAttribute){
        AttributeInfo attributeInfo = new AttributeInfo();
        String[] details = fileContent.split("\r\n");
        for(String attInfo : details){
            if (StringUtils.isBlank(attInfo) || !attInfo.contains("=") || attInfo.length() <=2) {
                continue;
            }
            String[] xx = attInfo.split("=");
            allAttribute.put(xx[0],xx.length>1?xx[1]:"");
            if(xx[0].equals("坐标系")){
                attributeInfo.setZbx(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("几度分带")){
                attributeInfo.setJdfd(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("投影类型")){
                attributeInfo.setTylx(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("计量单位")){
                attributeInfo.setJldw(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("带号")){
                attributeInfo.setDh(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("精度")){
                attributeInfo.setJd(xx.length>1?xx[1]:"");
            }else if(xx[0].equals("转换参数")){
                attributeInfo.setZhcs(xx.length>1?xx[1]:"");
            }
        }
        return attributeInfo;
    }


    public List<Map<String,Object>> getCoordinates(String fileContent){
        List<Map<String,Object>> coordinateList = new ArrayList<>();
        String[] details = fileContent.split("\r\n");
        List<List<double[]>> rings = null;
        Map<String,Object> geoMap = null;
        /**圈号*/
        String qh = "";
        for(String coordinateInfo : details){
            if(StringUtils.isBlank(coordinateInfo)){
                continue;
            }
            if(coordinateInfo.contains("@")){
                if(rings != null){
                    /**构建上一块的数据**/
                    Map<String,Object> spatialReferenceMap = new HashMap<>(2);
                    spatialReferenceMap.put("wkid",coordinate_wkId);
                    spatialReferenceMap.put("latestWkid",coordinate_wkId);
                    Map<String,Object> geometryMap = new HashMap<>(2);
                    geometryMap.put("rings",rings);
                    geometryMap.put("spatialReference",spatialReferenceMap);
                    Map<String,Object> geometryArrayMap = new HashMap<>(1);
                    geometryArrayMap.put("Geometry",geometryMap);
                    List<Map> arrayMaps = new ArrayList<>();
                    arrayMaps.add(geometryArrayMap);
                    geoMap.put("GeometryArray",arrayMaps);
                    coordinateList.add(geoMap);
                    qh = "";
                }
                /**构建新地块信息**/
                rings = new ArrayList<>();
                geoMap = new HashMap<>(1);
                /**增加总体描述**/
                String[] landPieceDesc = coordinateInfo.split(",");
                String dkmj = landPieceDesc[1].replaceAll("[\\t\\n\\r]","");
                String dkmc = landPieceDesc[3].replaceAll("[\\t\\n\\r]","");
                if(StringUtils.isBlank(dkmc)){
                    dkmc = "地块"+(rings.size()==0?1:rings.size()+1);
                }
                String dkbh = landPieceDesc[2].replaceAll("[\\t\\n\\r]","");
                String dkyt = landPieceDesc[6].replaceAll("[\\t\\n\\r]","");
                String tfh = landPieceDesc[5].replaceAll("[\\t\\n\\r]","");
                String jzds = landPieceDesc[0].replaceAll("[\\t\\n\\r]","");
                Map<String,Object> landDesc = new HashMap<>();
                landDesc.put("dkmj",dkmj);
                landDesc.put("dkmc",dkmc);
                landDesc.put("dkbh",dkbh);
                landDesc.put("dkyt",dkyt);
                landDesc.put("tfh",tfh);
                landDesc.put("jzds",jzds);
                geoMap.put("landDesc",landDesc);
            }else {
                String[] coor = coordinateInfo.split(",");
                String coordinate_index = coor[1];
                if(rings.size() == 0 || !qh.equals(coordinate_index)){
                    qh = coordinate_index;
                    rings.add(new ArrayList<>());
                }
                /**获取最新的坐标集合**/
                List<double[]> ring = rings.get(rings.size() - 1);
                Double coordinate_first = Double.valueOf(coor[2]);
                Double coordinate_second = Double.valueOf(coor[3]);
                double[] coordinate = {coordinate_second,coordinate_first};
                ring.add(coordinate);
            }
        }
        Map<String,Object> spatialReferenceMap = new HashMap<>(2);
        spatialReferenceMap.put("wkid",coordinate_wkId);
        spatialReferenceMap.put("latestWkid",coordinate_wkId);

        Map<String,Object> geometryMap = new HashMap<>(2);
        geometryMap.put("rings",rings);
        geometryMap.put("spatialReference",spatialReferenceMap);

        Map<String,Object> geometryArrayMap = new HashMap<>(1);
        geometryArrayMap.put("Geometry",geometryMap);
        List<Map> arrayMaps = new ArrayList<>();
        arrayMaps.add(geometryArrayMap);
        geoMap.put("GeometryArray",arrayMaps);
        coordinateList.add(geoMap);
        return coordinateList;
    }
}
